package com.ruoyi.system.outto.goods.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.GlobalException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.V2.goodInfo.mapper.GoodsInfoImgMapper;
import com.ruoyi.system.V2.inter.mapper.ArtRecMapper;
import com.ruoyi.system.model.domain.ArtRec;
import com.ruoyi.system.model.domain.GameAttribute;
import com.ruoyi.system.model.domain.GoodsInfoImg;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.model.dto.GoodsInfoAndAttrValueDto;
import com.ruoyi.system.outto.core.LCConstant;
import com.ruoyi.system.outto.core.MapUntils;
import com.ruoyi.system.outto.core.OutUrlConstant;
import com.ruoyi.system.outto.dto.HttpUtil;
import com.ruoyi.system.outto.game.CommonApi;
import com.ruoyi.system.outto.goods.NEWQQTAPI;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Author sdq
 * @Date 2022/8/31 22:28
 */
@Slf4j
@Service
public class NEWQQTAPIImpl implements NEWQQTAPI {
    @Resource
    private CommonApi commonApi;
    @Resource
    private GoodsInfoImgMapper goodsInfoImgMapper;
    @Resource
    private GameAttributeMapper gameAttributeMapper;
    @Resource
    private ArtRecMapper artRecMapper;

    private Map<String, String> params(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner salePartner){
        Map<String,String> params = new HashMap<>();

        params.put("orderNo", goodsInfoAndAttrValueDto.getId().toString());//账号编号
        params.put("des", goodsInfoAndAttrValueDto.getDes());
        BigDecimal bigDecimal = commonApi.addPrice(goodsInfoAndAttrValueDto.getPrice(), salePartner.getSeleep());
        commonApi.setPrice(bigDecimal, goodsInfoAndAttrValueDto.getId(), salePartner);
        params.put("price", String.valueOf(bigDecimal));
        params.put("extractAccount", StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getAccountNumber())?goodsInfoAndAttrValueDto.getAccountNumber():"zh"+goodsInfoAndAttrValueDto.getId());
        params.put("extractPwd", "123456");
        params.put("extractTel","123456");
        params.put("realPrice", String.valueOf(goodsInfoAndAttrValueDto.getCostPrice()));
        params.put("remake", goodsInfoAndAttrValueDto.getRemark());
        params.put("titleImg", goodsInfoAndAttrValueDto.getTitleImg()==null||goodsInfoAndAttrValueDto.getTitleImg().equals("")?LCConstant.defaultImg:goodsInfoAndAttrValueDto.getTitleImg());
        String imgList = "";
        GoodsInfoImg goodsInfoImg = new GoodsInfoImg();
        goodsInfoImg.setGoodsId(goodsInfoAndAttrValueDto.getId());
        goodsInfoImg.setType(Constants.STR_1);
        List<GoodsInfoImg> infoImgs = goodsInfoImgMapper.selectGoodsInfoImgList(goodsInfoImg);
        if(infoImgs.size()>0){
            for(GoodsInfoImg goodsInfoImg1:infoImgs){
                imgList+=","+goodsInfoImg1.getImgUrl();
            }
        }else{
            imgList = ","+LCConstant.defaultImg;
        }
        params.put("imgList", imgList.substring(1));
        GameAttribute gameAttribute = null;
        if(goodsInfoAndAttrValueDto.getGameName().contains("王者荣耀")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF16()));
            params.put("regional",goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName().replace("QQ","Q").replace("微信","WX"));
            if(gameAttribute.getFieldName().contains("可二次实名")||gameAttribute.getFieldName().contains("已删实名")) {
                params.put("level", "有二次");
            }else{
                params.put("level", "包人脸");
            }
            params.put("type", goodsInfoAndAttrValueDto.getGameName());
            params.put("heroes", StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF7())?"0":goodsInfoAndAttrValueDto.getF7());
            params.put("skins", StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF8())?"0":goodsInfoAndAttrValueDto.getF8());
            params.put("suit", "0");
            params.put("guns", "0");
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("和平精英")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
            params.put("regional",goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName().replace("QQ","Q").replace("微信","WX"));
            if(gameAttribute.getFieldName().contains("可二次实名")||gameAttribute.getFieldName().contains("已删实名")) {
                params.put("level", "有二次");
            }else{
                params.put("level", "包人脸");
            }
            params.put("type", goodsInfoAndAttrValueDto.getGameName());
            params.put("heroes", "0");
            params.put("skins", "0");
            params.put("suit", StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF8())?"0":goodsInfoAndAttrValueDto.getF8());
            params.put("guns", StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF9())?"0":goodsInfoAndAttrValueDto.getF9());
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("原神")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF20()));
            if(goodsInfoAndAttrValueDto.getCarrierName().equals("游戏官方")) {
                params.put("regional", "官服");
            }else{
                params.put("regional", "B服");
            }
            if(gameAttribute.getFieldName().contains("未绑定邮箱")) {
                params.put("level", "未绑定邮箱");
            }else if(gameAttribute.getFieldName().contains("可修改邮箱")||gameAttribute.getFieldName().contains("已删人脸实名可随时认证")){
                params.put("level", "邮箱未实名");
            }else{
                params.put("level", "邮箱已实名");
            }
            params.put("type", goodsInfoAndAttrValueDto.getGameName());
            params.put("heroes", "0");
            params.put("skins", "0");
            params.put("suit", "0");
            params.put("guns", "0");
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("英雄联盟")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF1()));
            params.put("regional",goodsInfoAndAttrValueDto.getServerName());
            if(gameAttribute.getFieldName().contains("有修改人脸机会")||gameAttribute.getFieldName().contains("已删人脸实名可随时认证")) {
                params.put("level", "有二次");
            }else{
                params.put("level", "包人脸");
            }
            params.put("type", goodsInfoAndAttrValueDto.getGameName());
            params.put("heroes", StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF3())?"0":goodsInfoAndAttrValueDto.getF3());
            params.put("skins", StringUtils.isNotEmpty(goodsInfoAndAttrValueDto.getF4())?"0":goodsInfoAndAttrValueDto.getF4());
            params.put("suit", "0");
            params.put("guns", "0");
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("穿越火线")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF3()));
            params.put("regional",goodsInfoAndAttrValueDto.getServerName());
            if(gameAttribute.getFieldName().contains("有修改机会")||gameAttribute.getFieldName().contains("已删实名")) {
                params.put("level", "有二次");
            }else{
                params.put("level", "包人脸");
            }
            params.put("type", "CF端游");
            params.put("heroes", "0");
            params.put("skins", "0");
            params.put("suit", "0");
            params.put("guns", "0");
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("火影忍者")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF7()));
            //实名未配置
            params.put("regional",goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName().replace("QQ","Q").replace("微信","WX"));
            params.put("type", goodsInfoAndAttrValueDto.getGameName());
            if(gameAttribute.getFieldName().equals("可改二次")){
                params.put("level", "有二次");
            }else if(gameAttribute.getFieldName().equals("人脸包赔")){
                params.put("level", "包人脸");
            }else{
                params.put("level", "无二次");
            }
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("枪战王者")){
            gameAttribute = gameAttributeMapper.selectGameAttributeById(Long.valueOf(goodsInfoAndAttrValueDto.getF10()));
            params.put("regional",goodsInfoAndAttrValueDto.getSystemName()+goodsInfoAndAttrValueDto.getCarrierName().replace("QQ","Q").replace("微信","WX"));
            if(gameAttribute.getFieldName().contains("有修改人脸机会")||gameAttribute.getFieldName().contains("已删人脸实名")) {
                params.put("level", "有二次");
            }else{
                params.put("level", "包人脸");
            }
            params.put("type", "CF手游");
            params.put("heroes", "0");
            params.put("skins", "0");
            params.put("suit", StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF5())?"0":goodsInfoAndAttrValueDto.getF5());
            params.put("guns",StringUtils.isEmpty(goodsInfoAndAttrValueDto.getF6())?"0":goodsInfoAndAttrValueDto.getF6());
        }else if(goodsInfoAndAttrValueDto.getGameName().contains("蛋仔派对")){
            //实名未配置
            params.put("regional",goodsInfoAndAttrValueDto.getServerName());

        }
        return params;
    }
    private List<GameAttribute> getGameAttributs(String ids) {
        return gameAttributeMapper.selectGameAttributeListByIds((Long[]) ConvertUtils.convert(ids.split(","),
                Long.class));
    }
    private String reIds(String allIds, String ids) {
        if (StringUtils.isNotEmpty(ids)) {
            if (StringUtils.isNotEmpty(allIds)) {
                allIds = allIds + "," + ids;
            } else {
                allIds = ids;
            }
        }
        return allIds;
    }
    @Override
    public AjaxResult addGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto,  SalePartner salePartner) {
        try{
            Map<String, Object> result = new HashMap<>();
            Map<String, String> header = new HashMap<>();
            header.put("author",salePartner.getSaleKey());
            Map<String, String> params = params(goodsInfoAndAttrValueDto,salePartner);
            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(params), JSONObject.class);
            List<JSONObject> list = new ArrayList<>();
            list.add(jsonObject);
            String string = JSONObject.toJSONString(list);

//            HttpUtil.doGet(baseUrl, params,header);
//            Gson gson = new Gson();
//            String json = gson.toJson(params);
            log.info("虞都旧接口新增请求参数={}",string);
            String response = HttpUtils.sendYDPost(salePartner.getOther()+"/api/account/save",string, salePartner.getSaleKey());
            log.info("虞都旧接口新增返回参数={}",response);
            JSONObject resultJson = JSONObject.parseObject(response);
//            String msg = resultJson.getString("msg");
            if (!"200".equals(resultJson.getString("code"))){
                result.put("code",500);
            }else {
                result.put("code",200);
//                String accountId = JSONObject.parseObject(resultJson.getString("data")).getString("accountId");
                ArtRec artRecreq = new ArtRec();
                artRecreq.setConfig(salePartner.getId());
                artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
                if(artRecs.size()>0) {
                    artRecs.get(0).setOutid(String.valueOf(goodsInfoAndAttrValueDto.getId()));
                    artRecMapper.updateArtRec(artRecs.get(0));
                }else{
                    artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                    artRecreq.setCreateId(goodsInfoAndAttrValueDto.getCreateId());
                    artRecreq.setType(LCConstant.interAdd);
                    artRecreq.setConfig(salePartner.getId());
                    artRecreq.setSaleCode(salePartner.getSaleName());
                    artRecreq.setOutid(String.valueOf(goodsInfoAndAttrValueDto.getId()));
                    artRecMapper.insertArtRec(artRecreq);
                }
            }
            result.put("req",JSONObject.toJSONString(params));
            result.put("res",MapUntils.unicodeToString(response));
            result.put("msg",response);
            return AjaxResult.success(result);
        }catch (Exception e){
            throw new GlobalException(e.getMessage());
        }
    }

    @Override
    public AjaxResult editGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto,  SalePartner salePartner) {
        try{
            Map<String, Object> result = new HashMap<>();
            Map<String,String> header = new TreeMap<>();
            header.put("author",salePartner.getSaleKey());
            Map<String, String> params = params(goodsInfoAndAttrValueDto,salePartner);
//            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(params), JSONObject.class);
//            List<JSONObject> list = new ArrayList<>();
//            list.add(jsonObject);
//            String string = JSONObject.toJSONString(list);
            log.info("虞都旧接口编辑请求参数={}",params);
            String response = HttpUtils.sendYDPost(salePartner.getOther()+"/api/account/edit",JSONObject.toJSONString(params), salePartner.getSaleKey());
            log.info("虞都旧接口编辑返回参数={}",response);
            JSONObject resultJson = JSONObject.parseObject(response);
            String msg = resultJson.getString("msg");
//            msg = MapUntils.unicodeToString(msg);

            if (!"200".equals(resultJson.getString("code"))){
                result.put("code",500);
            }else {
                result.put("code",200);
            }
            result.put("req",JSONObject.toJSONString(params));
            result.put("res",response);
            result.put("msg",msg);
            return AjaxResult.success(result);
        }catch (Exception e){
            throw new GlobalException(e.getMessage());
        }
    }

    @Override
    public AjaxResult delGame(GoodsInfoAndAttrValueDto goodsInfoAndAttrValueDto, SalePartner sa) {
        try{
            Map<String, Object> result = new HashMap<>();
            Map<String,String> header = new TreeMap<>();
            header.put("author",sa.getSaleKey());
            Map<String, String> params = new HashMap<>();
            params.put("orderNo", String.valueOf(goodsInfoAndAttrValueDto.getId()));
//            JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(params), JSONObject.class);
//            List<JSONObject> list = new ArrayList<>();
//            list.add(jsonObject);
//            String string = JSONObject.toJSONString(list);
            log.info("虞都旧接口删除请求参数={}",params);
            String response = HttpUtils.sendYDPost(sa.getOther()+"/api/account/undercarriage",JSONObject.toJSONString(params), sa.getSaleKey());
            log.info("虞都旧接口删除返回参数={}",response);
            JSONObject resultJson = JSONObject.parseObject(response);
            String msg = resultJson.getString("msg");
//            msg = MapUntils.unicodeToString(msg);
            if (!"200".equals(resultJson.getString("code"))){
                result.put("code",500);
            }else {
                result.put("code",200);
                ArtRec artRecreq = new ArtRec();
                artRecreq.setConfig(sa.getId());
                artRecreq.setAccountId(goodsInfoAndAttrValueDto.getId());
                List<ArtRec> artRecs = artRecMapper.selectArtRecList(artRecreq);
                if(artRecs.size()>0) {
                    artRecMapper.deleteArtRecById(artRecs.get(0).getId());
                }
            }
            result.put("req",JSONObject.toJSONString(params));
            result.put("res",MapUntils.unicodeToString(response));
            result.put("msg",msg);
            return AjaxResult.success(result);
        }catch (Exception e){
            throw new GlobalException(e.getMessage());
        }
    }

    public static String getCover(Map<String,Object> params){
        StringBuffer resut = new StringBuffer();
        for (String key : params.keySet()) {
            if(params.get(key) instanceof String && StringUtils.isNotEmpty((String)params.get(key))){
                resut.append(key+"="+params.get(key)+"&");
            }
            if(params.get(key) instanceof Boolean){
                resut.append(key+"="+params.get(key)+"&");
            }
            if(params.get(key) instanceof Long){
                resut.append(key+"="+params.get(key)+"&");
            }
            if(params.get(key) instanceof Integer && null != params.get(key)){
                resut.append(key+"="+params.get(key)+"&");
            }
        }
        return resut.toString();
    }

    public static void main(String[] args) {
        String response = getAtta();
        System.out.println(MapUntils.unicodeToString(response));

    }

    public static String getAtta(){
        Map<String,String> params = new TreeMap<>();
        Map<String,String> header = new TreeMap<>();
        header.put("author","DAA709BDEB5C709BA02664B71D166C21");
        String baseUrl = "http://back.qiuqiutong.com/api/account/getReal/蛋仔派对";
//        params.put("gameId",gameId);
        String response = HttpUtil.doGet(baseUrl, params,header);
        return response;
    }
}
